import pathOr from 'ramda/es/pathOr';
import { name } from '../../../../../package.json';


export const getTaskEnable = (taskType, triggerType) => {
	const resD = JSON.parse(sessionStorage.resD || null) || {};
	if (triggerType == "manual") {
		const count = pathOr(0, ['balance', `${taskType}ManualTask`, 'total'], resD) - pathOr(0, ['balance', `${taskType}ManualTask`, 'used'], resD)
		return pathOr(0, ['feature', `${taskType}ManualTask`], resD) && count > 0
	} else {
		return pathOr(0, ['feature', `${taskType}Task`], resD)
	}
}

export const checkHttps = (jobMap, jobIds) => {
	const hasHttps = jobIds?.length > 0
		? jobIds.reduce((t, i) => {
			if (jobMap[i]?.split('://')[0] === 'https') {
				t += 1;
			}
			return t;
		}, 0)
		: true;
	return Boolean(hasHttps)
}

export const isSslEnable = (triggerType, jobIds, manualJobMap) => {
	const enableType = getTaskEnable("ssl", triggerType)
	const hasHttps = checkHttps(manualJobMap, jobIds)
	return Boolean(enableType && hasHttps)
}

// 监测深度参数
export const getMaxDepthOptions = (taskType) => {
	const resD = JSON.parse(sessionStorage.resD || null) || {};
	const maxDepth = pathOr(0, ['balance', `${taskType}MaxDepth`, 'total'], resD)
	const defaultMaxDepth = window.kuiper?.app?.config[name].settingOptions.depthOptions
	const resMaxDepth = Array.from(new Set(defaultMaxDepth.reduce((t,i) => {
		if (i.value < maxDepth) t.push(i.value)
		return t
	}, []).concat(maxDepth))).sort((a, b) => a - b).map(
		item => {
			return { label: item.toString(), value: item };
		}
	)
	return resMaxDepth
}

// 监测最大页数参数
export const getMaxPageOptions = (taskType) => {
	const resD = JSON.parse(sessionStorage.resD || null) || {};
	const maxPage = pathOr(0, ['balance', `${taskType}MaxPage`, 'total'], resD)
	const defaultMaxPage = window.kuiper?.app?.config[name].settingOptions.maxPageOptions
	const resMaxPage = Array.from(new Set(defaultMaxPage.reduce((t,i) => {
		if (i < maxPage) {
			t.push(i)
		}
		return t
	}, []).concat(maxPage))).sort((a, b) => a - b).map(
		item => {
			return { label: item.toString(), value: item };
		}
	)
	return resMaxPage
}

// 篡改监测基准更新频率参数
export const getChangeBaseOptions = () => {
	const resD = JSON.parse(sessionStorage.resD || null) || {};
	const changeBase = pathOr(0, ['balance', 'changeCheckMaxBaseChange', 'total'], resD)
	const defaultChangeBase = [2, 3, 4, 5]
	const resChangeBase = Array.from(new Set(defaultChangeBase.reduce((t,i) => {
		if (i >= changeBase) {
			t.push(i)
		}
		return t
	}, []).concat(changeBase))).sort((a, b) => a - b).map(
		item => {
			return { label: item.toString(), value: item };
		}
	)
	return resChangeBase
}


export default getTaskEnable;
